package pers.qianyu.month_202102.date_20210222;

/**
 * 66. 加一
 * https://leetcode-cn.com/problems/plus-one/
 *
 * @author mizzle rain
 * @date 2021-02-22 12:15
 */
public class PlusOne {
    public int[] plusOne(int[] digits) {
        int n = digits.length;
        int take = 0;
        for (int i = n - 1; i >= 0; i--) {
            int sum = digits[i] + take;
            if (i == n - 1) sum++;
            if (sum >= 10) {
                digits[i] = sum % 10;
                take = 1;
            } else {
                digits[i] = sum;
                take = 0;
                break;
            }
        }
        if (take == 1) {
            int[] res = new int[n + 1];
            res[0] = 1;
            System.arraycopy(digits, 0, res, 1, n);
            return res;
        }
        return digits;
    }
}
